# views.py: 路由，视图函数
# 蓝图
from flask import Blueprint,request,render_template

from .models import *
import random
blue = Blueprint('user',__name__)
# blue2 = Blueprint('product',__name__)
@blue.route('/')
def index1():
    return '<h1>index1</h1>'


# -------------------------------一对多----------------------------------
@blue.route('/addgrade/')
def add_grade():
    # 添加班级
    grades = []
    for i in range(10):
        grade = Grade()
        grade.name = f'java{i}-{str(random.randint(10,99))}'
        grades.append(grade)
        try:
            db.session.add_all(grades)
            db.session.commit()
        except Exception as e:
            print('e:',e)
            db.session.rollback()
            db.session.flush()
    return "OK"

@blue.route('/addstu/')
def add_stu():
    # 添加学生
    students = []
    for i in range(10,20):
        student = Student()
        student.name = f'stu{i}'
        student.gradeid = random.randint(1,2)
        students.append(student)
        try:
            db.session.add_all(students)
            db.session.commit()
        except Exception as e:
            print('e:',e)
            db.session.rollback()
            db.session.flush()
    return "OK"

@blue.route('/updatestu/')
def update_stu():
    stu = Student.query.first()
    stu.age = 100
    db.session.commit()
    return 'OK'

@blue.route('/delstu/')
def del_stu():
    stu = Student.query.first()
    db.session.delete(stu)
    db.session.commit()
    return 'OK'

@blue.route('/delgrade/')
def del_grade():
    grade = Grade.query.first()
    db.session.delete(grade)
    db.session.commit()
    return 'OK'

# 查询
@blue.route('/getstu/')
def get_stu():
    # 查询学生所在班级,反向引用grade
    stu = Student.query.get(21)
    print(stu.name,stu.age,stu.gradeid,stu.grade,stu.grade.id, stu.grade.name)
    # 查询某班级下的所有学生
    grade = Grade.query.get(1)
    print(grade.name)
    print(grade.students)
    for stu in grade.students:
        print(stu.name,stu.age)
    return 'success'


@blue.route('/addage/')
def add_age():
    students = Student.query.filter(Student.id >= 20, Student.id <= 29).all()
    # 批量更新 age
    for stu in students:
        stu.age = random.randint(18, 25)  # 或者你想赋的固定值
        # 提交事务
        db.session.commit()
    return 'success'


# -------------------------------多对多----------------------------------
@blue.route('/adduser/')
def add_user():
    # 添加用户
    users = []
    for i in range(10,14):
        user = UserModel()
        user.name = f'小吴{i}'
        user.age = i
        users.append(user)
        try:
            db.session.add_all(users)
            db.session.commit()
        except Exception as e:
            print('e:',e)
            db.session.rollback()
            db.session.flush()
    return "OK"


@blue.route('/addmovie/')
def add_movie():
    # 添加电影
    movies = []
    for i in range(10,14):
        movie = Movie()
        movie.name = f'阿凡达{i}'
        movies.append(movie)
        try:
            db.session.add_all(movies)
            db.session.commit()
        except Exception as e:
            print('e:',e)
            db.session.rollback()
            db.session.flush()
    return "OK"

@blue.route('/addcollect/')
def add_collect():
    # 用户收藏电影
    user = UserModel.query.get(1)
    movie =Movie.query.get(1)
    user.movies.append(movie)
    db.session.commit()
    return 'success'

@blue.route('/getcollect/')
def get_collect():
    # 查找某用户收藏的所有电影
    user = UserModel.query.get(1)
    print(user.movies)

    # 查找收藏了某电影的所有用户
    movie = Movie.query.get(1)
    print(movie.users)
    print(list(movie.users))
    return 'success'

# 修改：与单表操作相同
# 删除
@blue.route('/deluser/')
def del_user():
    # 级联删除
    user = UserModel.query.get(1)
    db.session.delete(user)
    db.session.commit()
    return 'success'
